package com.watchme.parser.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * Created by wangqiang on 2017/10/26.
 */
public class ReadColorUtil {

    private static ReadColorUtil instance = null;

    private ReadColorUtil() {
    }

    private static Integer LOCK = 0;

    public static ReadColorUtil getInstance() {
        synchronized (LOCK) {
            if (instance == null) {
                instance = new ReadColorUtil();
            }
            return instance;
        }
    }


    public boolean isChromatic(String imagePath) {
        int x = 0;
        try {
            x = getScreenPixel(100, 345);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        //System.out.println(x);
        return getImagePixel(imagePath);
    }

    /**
     * 读取一张图片的RGB值，返回是否彩色
     *
     * @throws Exception
     */
    private boolean getImagePixel(String imagePath) {
        int[] rgb = new int[3];
        File file = new File(imagePath);
        String fileName = file.getName();
        BufferedImage bi = null;
        try {
            long now = System.currentTimeMillis();
            bi = ImageIO.read(file);
            int width = bi.getWidth();
            int height = bi.getHeight();
            int minx = bi.getMinX();
            int miny = bi.getMinY();
            //System.out.println("width=" + width + ",height=" + height + ".");
            //System.out.println("minx=" + minx + ",miniy=" + miny + ".");
            boolean result = false;
            for (int i = minx; i < width; i++) {
                for (int j = miny; j < height; j++) {
                    int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
                    rgb[0] = (pixel & 0xff0000) >> 16;
                    rgb[1] = (pixel & 0xff00) >> 8;
                    rgb[2] = (pixel & 0xff);
                    if (rgb[0] > 100) {
                        if (rgb[0] > (rgb[1] + rgb[2])) {
                            result = true;
                            break;
                        }
                    }
                    //System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","+ rgb[1] + "," + rgb[2] + ")");
                }
            }
            long s = System.currentTimeMillis() - now;
            System.out.println(fileName + "图片" + (result ? "彩色" : "黑白") + ",耗时：" + s);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    /**
     * 返回屏幕色彩值
     *
     * @param x
     * @param y
     * @return
     * @throws AWTException
     */
    private int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
        Robot rb = null; // java.awt.image包中的类，可以用来抓取屏幕，即截屏。
        rb = new Robot();
        Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
        Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
        //System.out.println(di.width);
        //System.out.println(di.height);
        Rectangle rec = new Rectangle(0, 0, di.width, di.height);
        BufferedImage bi = rb.createScreenCapture(rec);
        int pixelColor = bi.getRGB(x, y);

        return 16777216 + pixelColor; // pixelColor的值为负，经过实践得出：加上颜色最大值就是实际颜色值。
    }

    /**
     * @param args
     */
//    public static void main(String[] args) throws Exception {
//        String image = "D:\\0.jpg";
//        System.out.println(ReadColorUtil.getInstance().isChromatic(image));
//    }

}
